"use strict";
cc._RF.push(module, 'b48e3F1HZFDlLlDB11eczyr', 'LevelBtn');
// scripts/Game/Obj/LevelBtn.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameConfig_1 = require("../../engines/configs/gameConfig");
var gameEngine_1 = require("../../engines/gameEngine");
var gameMode_json_1 = require("../../engines/managers/gameContent/gameMode_json");
var userInterface_1 = require("../../engines/services/userContent/userInterface");
var ClickBase_1 = require("../Base/ClickBase");
var Enum_1 = require("../Enum");
var MainRoot_1 = require("../MainRoot");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property, menu = _a.menu, disallowMultiple = _a.disallowMultiple;
var LevelBtn = /** @class */ (function (_super) {
    __extends(LevelBtn, _super);
    function LevelBtn() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.m_levelNum = 1;
        _this.lockBg = null;
        _this.selcetSp = null;
        _this.m_numLabel = null;
        _this.m_mode = gameMode_json_1.GAME_MODE.NORMAL;
        _this.lastBtn = null;
        _this.levelObj = null;
        _this.ShowImg = null;
        _this.isStartGame = false;
        _this.subPowerLb = null;
        _this.powerNode = null;
        _this.lb_level = null;
        _this.levelLayout = null;
        _this.levelLb = null;
        _this.imgStr = '';
        return _this;
    }
    Object.defineProperty(LevelBtn.prototype, "locked", {
        get: function () {
            // return this.m_levelNum>mainRoot.LevelSystem.GetFarLevel(this.m_mode);
            return MainRoot_1.default.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum) < 0 && !gameEngine_1.default.getServices().userSrv.getLevelAllUnlockFlag();
        },
        enumerable: false,
        configurable: true
    });
    LevelBtn.prototype.onLoad = function () {
        var _this = this;
        this.node.on(cc.Node.EventType.TOUCH_START, this.ClickDown, this);
        // this.node.on(cc.Node.EventType.TOUCH_END, this.ClickUp, this);
        this.node.on(cc.Node.EventType.TOUCH_START, this.OnDown, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.OnUp, this);
        // this.node.on(cc.Node.EventType.TOUCH_END, this.OnUp, this);
        gameEngine_1.default.on('refrashAll', function () {
            _this.Refresh();
        }, this);
    };
    LevelBtn.prototype.Init = function (levelNum, mode, obj) {
        return __awaiter(this, void 0, void 0, function () {
            var flag2;
            return __generator(this, function (_a) {
                this.m_mode = mode;
                this.levelObj = obj;
                // console.log("关卡:",obj);
                this.m_levelNum = levelNum;
                this.ShowImg = this.node.getChildByName("Layer").getChildByName("showImg").getComponent(cc.Sprite);
                this.m_numLabel = this.node.getChildByName("Layer").getChildByName("LevelNum").getComponent(cc.Label);
                this.lb_level = this.node.getChildByName("Layer").getChildByName("lbLevel").getComponent(cc.Label);
                this.levelLayout = this.node.getChildByName("level");
                this.levelLb = this.levelLayout.getChildByName("labelLevel").getComponent(cc.Label);
                switch (mode) {
                    case gameMode_json_1.GAME_MODE.NORMAL: {
                        this.levelLb.string = (gameEngine_1.default.getManagers().peopleArcheryManager.getData().infinityLevel + 1).toString() + "关";
                        break;
                    }
                }
                // this.lb_level.string = this.m_levelNum.toString();
                this.m_numLabel.string = obj["levelTitle"]; //levelNum.toString();
                this.imgStr = obj["levelImg"];
                if (this.imgStr != null && this.imgStr != '') {
                    this.LoadImg();
                }
                this.powerNode = this.node.getChildByName("power");
                this.lockBg = this.node.getChildByName("lockBg");
                this.selcetSp = this.node.getChildByName("select");
                this.subPowerLb = this.powerNode.getChildByName("subPowerLb").getComponent(cc.Label);
                this.subPowerLb.string = "-" + obj["subPower"];
                this.levelLayout.active = this.levelObj["infinityFlag"];
                if (this.locked) {
                    this.lockBg.active = true;
                    this.powerNode.active = false;
                    this.levelLayout.active = false;
                }
                flag2 = this.m_levelNum == MainRoot_1.default.LevelSystem.GetFarLevel(this.m_mode);
                if (flag2) {
                    this.selcetSp.active = true;
                }
                return [2 /*return*/];
            });
        });
    };
    LevelBtn.prototype.LoadImg = function () {
        var _this = this;
        MainRoot_1.default.frameBundle.load("Img/Page/LevelMenu/showImg/" + this.imgStr, cc.Texture2D, function (e, Ass) {
            if (e) {
                return;
            }
            var spriteFrame = new cc.SpriteFrame(Ass);
            _this.ShowImg.node.active = true;
            _this.ShowImg.spriteFrame = spriteFrame;
        });
    };
    LevelBtn.prototype.Refresh = function () {
        // console.log("!!!!!!!!!刷新");
        // let flag = this.m_levelNum<=mainRoot.LevelSystem.GetFarLevel(this.m_mode);
        this.isStartGame = false;
        var flag = MainRoot_1.default.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum) >= 0 || gameEngine_1.default.getServices().userSrv.getLevelAllUnlockFlag();
        var flag2 = this.m_levelNum == MainRoot_1.default.LevelSystem.GetFarLevel(this.m_mode); //|| (mainRoot.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum + 1) < 0 && !this.locked);//this.m_levelNum==mainRoot.LevelSystem.GetFarLevel(this.m_mode);
        // console.log(this.m_levelNum, mainRoot.LevelSystem.GetFarLevel(this.m_mode));
        // this.m_numLabel.node.parent.active = flag;
        this.powerNode.active = false;
        if (flag) {
            this.lockBg.active = false;
            this.powerNode.active = true;
            this.levelLayout.active = this.levelObj["infinityFlag"];
            // node_component.loadResourcesPicture(`Img/Page/LevelMenu/${flag2?'farLevel':'unlock'}`,this.node,mainRoot.frameBundle);
        }
        else {
            this.lockBg.active = true;
            this.powerNode.active = false;
            this.levelLayout.active = false;
        }
        if (flag2) {
            this.selcetSp.active = true;
        }
    };
    LevelBtn.prototype.ClickDown = function () {
        // this.node.scale = 1.1;
    };
    LevelBtn.prototype.ClickUp = function () {
        return __awaiter(this, void 0, void 0, function () {
            var prams, needPower;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        gameEngine_1.default.getManagers().soundMgr.playSound('mainSound/anNiu').then();
                        gameEngine_1.default.getManagers().soundMgr.playVibrate();
                        if (!!this.isStartGame) return [3 /*break*/, 2];
                        // if(this.m_levelNum == mainRoot.LevelSystem.GetFarLevel(this.m_mode)+1){
                        if (MainRoot_1.default.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum) < 0 && !gameEngine_1.default.getServices().userSrv.getLevelAllUnlockFlag()) {
                            prams = {
                                mode: this.m_mode,
                                unlockCall: function () {
                                    MainRoot_1.default.LevelSystem.ChangeLevel(_this.m_levelNum, _this.m_mode);
                                    _this.Refresh();
                                    _this.lastBtn && _this.lastBtn.Refresh();
                                    MainRoot_1.default.UIModel.showMessageBox("\u7B2C" + _this.m_levelNum + "\u5173\u5DF2\u89E3\u9501");
                                },
                                isShowOne: true,
                                allCall: function () {
                                    MainRoot_1.default.UIModel.showMessageBox("\u6240\u6709\u5173\u5361\u5DF2\u89E3\u9501");
                                    MainRoot_1.default.LevelSystem.unAllLock(_this.m_mode);
                                    gameEngine_1.default.emit('refrashAll');
                                },
                                cancel: function () {
                                    _this.isStartGame = false;
                                }
                            };
                            MainRoot_1.default.UISystem.OpenPage(Enum_1.PAGE.UNLOCKPAGE, prams);
                            // modules().uiControlModule.showDialogBox("解锁关卡", "欣赏小视频解锁这一关", true, (ok) => {
                            // if (ok) {
                            //     mainRoot.LevelSystem.ChangeLevel(this.m_levelNum, this.m_mode);
                            //     this.Refresh();
                            //     this.lastBtn && this.lastBtn.Refresh();
                            //     mainRoot.UIModel.showMessageBox(`第${this.m_levelNum}关已解锁`);
                            // } else {
                            //     this.isStartGame = false;
                            // }
                            // }, null, "欣赏", "不了", 1, 0, "看视频提前解锁关卡");
                            return [2 /*return*/];
                        }
                        needPower = this.levelObj["subPower"] || 1;
                        if (gameEngine_1.default.getServices().userSrv.getPower() - needPower < 0) {
                            MainRoot_1.default.UIModel.showUserPropGet(userInterface_1.REWARD_TYPE.POWER, gameConfig_1.default.ADD_ONCE_POWER);
                            return [2 /*return*/];
                        }
                        this.isStartGame = true;
                        // if(this.locked){
                        //     mainRoot.UIModel.showMessageBox("请解锁前面的关卡");
                        //     return;
                        // }
                        // mainRoot.LevelSystem.ChangeLevel(this.m_levelNum);
                        // console.warn('重置2');
                        MainRoot_1.default.GotTips = false;
                        MainRoot_1.default.GotAnswer = false;
                        // managers().soundMgr.stopMusic();
                        // O1HD.getManagers().soundMgr.playSound('mainSound/inLevel').then();
                        return [4 /*yield*/, MainRoot_1.default.LevelSystem.LoadLevel(MainRoot_1.default.LevelSystem.GetSignleLevelInfo(this.m_mode, this.m_levelNum).bundleName, this.m_mode, this.m_levelNum, true, this.levelObj["infinityFlag"] ? this.levelObj : null)];
                    case 1:
                        // managers().soundMgr.stopMusic();
                        // O1HD.getManagers().soundMgr.playSound('mainSound/inLevel').then();
                        _a.sent();
                        // console.info("#记录按钮操作");
                        gameEngine_1.services().userSrv.setPower(-needPower, true, true);
                        MainRoot_1.default.UISystem.ClosePage(Enum_1.PAGE.LEVELMENU);
                        MainRoot_1.default.UISystem.ClosePage(Enum_1.PAGE.MODEMENU);
                        this.scheduleOnce(function () {
                            _this.isStartGame = false;
                        }, 1);
                        _a.label = 2;
                    case 2: return [2 /*return*/];
                }
            });
        });
    };
    LevelBtn.prototype.OnDowning = function (dt) {
    };
    LevelBtn = __decorate([
        ccclass,
        menu('工具组件/选关按钮')
    ], LevelBtn);
    return LevelBtn;
}(ClickBase_1.default));
exports.default = LevelBtn;

cc._RF.pop();